package com.kgc.controller;

import com.kgc.entity.MenuInfo;
import com.kgc.entity.UserInfo;
import com.kgc.entity.vo.MenuInfoVo;
import com.kgc.service.MenuInfoService;
import com.kgc.utils.ConfigUtills;
import com.kgc.utils.ResultMapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@CrossOrigin
@Controller
public class MenuInfoController {

    @Autowired
    private MenuInfoService menuInfoService;

    @RequestMapping("/menu")
    @ResponseBody
    public Map<String, Object> getMenuInfoList(HttpSession session) {
        //从缓存中获取用户对象（当前登录的是那个用户）
        UserInfo userInfo = (UserInfo) session.getAttribute(ConfigUtills.SESSION_USER);
        Map<String, Object> menuInfo = menuInfoService.getMenuInfo(userInfo.getId());
        return menuInfo;
    }

    @RequestMapping("/goMenuPage")
    public String goMenuInfoPage() {
        return "menu";
    }

    @RequestMapping("/getMenuInfo")
    @ResponseBody
    public ResultMapUtils getMenuInfo() {
        return ResultMapUtils.SUCCESS(menuInfoService.getMenuInfoAll());
    }

    @RequestMapping("/delMenu")
    @ResponseBody
    public ResultMapUtils delMenuInfo(Integer menuId,Integer status) {
        if (menuId == null) {
            return ResultMapUtils.ERROR("delm-1", "参数异常");
        }
        Integer pId=0;
        //如果status为1说明 做的是开启操作
        if (status==1){
            MenuInfo menuInfo = menuInfoService.getMenuInfoById(menuId);
            if (menuInfo.getPId()!=null){
                //如果menuInfo的Pid不为空，说明他是二级菜单，
                //需要把父级菜单的状态修改为1
                menuInfoService.delMenuInfoById(menuInfo.getPId(),status);
                pId=menuInfo.getPId();
            }
        }

        int count=menuInfoService.countMenuByPId(menuId);
        if (count>0){
            return ResultMapUtils.ERROR("delm-3","此菜单下有未关闭的二级菜单");
        }

        int i = menuInfoService.delMenuInfoById(menuId,status);
        if (i > 0) {
            return ResultMapUtils.SUCCESS("删除成功",pId);
        } else {
            return ResultMapUtils.ERROR("delm-2", "删除失败");
        }
    }

    /**
     * 新增 | 修改 公共的页面  需要解决的一个问题是：如何判断当前这个请求是新增还是修改
     * @param id 父级菜单的ID
     * @param pageType 页面类型  1 新增  2 修改一级菜单  其他 修改二级菜单
     * @param model
     * @return
     */
    @RequestMapping("/edit_menu_page")
    public String editMenuPage(Integer id,Integer pageType,Model model) {
        System.err.println("传递过来的ID："+id);
        List<MenuInfo> menuInfoList = new ArrayList<>();
        MenuInfo menuInfo=new MenuInfo();//被修改的对象
        if (pageType==1){
            if (id!=null){
                menuInfoList = menuInfoService.getMenuInfoOne();
                menuInfo.setPId(id);
            }
        }else {
            if (pageType!=2){
                menuInfoList = menuInfoService.getMenuInfoOne();
            }
            menuInfo=menuInfoService.getMenuInfoById(id);
        }
        model.addAttribute("menuInfoList", menuInfoList);
        model.addAttribute("menuInfo", menuInfo);
        model.addAttribute("pageType", pageType);
        return "edit_menu";
    }

    @PostMapping("/edit_menu")
    @ResponseBody
    public ResultMapUtils editMenu(Integer pageTye,MenuInfo menuInfo){
        if (pageTye==1){
            int i=menuInfoService.addMenuInfo(menuInfo);
            if (i>0){
                return ResultMapUtils.SUCCESS("新增成功！");
            }else {
                return ResultMapUtils.ERROR("editM-1","新增失败！");
            }
        }else {
            //修改
            int i=menuInfoService.updateMenuInfo(menuInfo);
            if (i>0){
                return ResultMapUtils.SUCCESS("修改成功！");
            }else {
                return ResultMapUtils.ERROR("editM-1","修改失败！");
            }
        }

    }

    /**
     * 查询菜单树
     * @param roleId 角色ID
     * @return
     */
    @RequestMapping("/getMenuTree")
    @ResponseBody
    public ResultMapUtils getMenuTree(Integer roleId){
        List<Map<String,Object>> maps=menuInfoService.getMenuTree(roleId);
        if (maps.size()>0){
            return ResultMapUtils.SUCCESS(maps);
        }else {
            return ResultMapUtils.ERROR("tree-1","查询菜单失败");
        }

    }

    /**
     * 查询菜单树
     * @param roleId 角色ID
     * @return
     */
    @RequestMapping("/getCMenuTree")
    @ResponseBody
    public ResultMapUtils getCMenuTree(Integer userId){
        List<Map<String,Object>> maps=menuInfoService.getCMenuTree(userId);
        if (maps.size()>0){
            return ResultMapUtils.SUCCESS(maps);
        }else {
            return ResultMapUtils.ERROR("tree-1","查询菜单失败");
        }

    }
}
